<div id="content">
  <div id="content-header">
    <div id="breadcrumb"> 
        <a href="#" class="tip-bottom" data-original-title="Go to Home"><i class="icon-home"></i> 首页</a> 
        <a href="#" class="tip-bottom" data-original-title="">配置管理</a> 
        <a href="#" class="current">酒水配置</a> 
    </div>
    
   <!--  <h1>Tables</h1> -->
  </div>
  <div class="container-fluid">
   <!--  <hr> -->
    <div class="row-fluid">
      <div class="span12">
        <div class="row-fluid">
            <button id="btnadd" class="btn btn-success pull-right">添&nbsp;加</button>
        </div>
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>酒水配置</h5>
          </div>
          <div class="widget-content nopadding">
            <table id="dataList" class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>酒水类别</th>
                  <th>酒水名称</th>
                  <th>价格</th>
                  <th>描述</th>
                  <th>排序号</th>
                  <th>状态</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>

              </tbody>
            </table>
          </div>
        </div>

        <div class="row-fluid">
            <div id="pagination" class="pagination alternate pull-right">
              
            </div>
        </div>

        </div>
      </div>
    </div>
  </div>
</div>


<div id="dialog-form" title="Create new user" style=" display: none;">
  <p class="validateTips"></p>
  <div id="userinfoedit">
   <form id="submitform" action="#" method="get" class="form-horizontal">
      <input type="hidden" id="d_uid"> 

      <table >
        <tr>
          <td>
            <div class="control-group">
              <label class="control-label">酒水类别 :</label>
              <div class="controls">
                <input id="typeId" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">酒水名称 :</label>
              <div class="controls">
                <input id="name" type="text" class="span11" placeholder="">
              </div>
            </div>
          </td>
        </tr>

        <tr>
          <td>
            <div class="control-group">
              <label class="control-label">价格 :</label>
              <div class="controls">
                <input id="price" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">描述 :</label>
              <div class="controls">
                <input id="description" type="text" class="span11" placeholder="">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
        </tr>

        <td>
            <div class="control-group">
              <label class="control-label">排序号 :</label>
              <div class="controls">
                <input id="orderType" type="text" class="span11" placeholder="">
              </div>
            </div>
          </td>
          <td>
            <div class="control-group">
              <label class="control-label">状态 :</label>
              <div class="controls">
                <input id="status" type="text" class="span11">
                <span class="help-block"></span>
              </div>
            </div>
          </td>
        </tr>

      </table>  
      
    </form>
  </div>
</div>

<script type="text/javascript">
  
$(function(){
    getDataList()
});

/**
* 初始化为空
*/
function Initialization(){

  var submitform=document.getElementById('submitform');
    
    submitform.typeId.value='';
    submitform.name.value='';
    submitform.price.value='';
    submitform.description.value='';
    submitform.orderType.value='';
    submitform.status.value='';
}
  
 var btnadd=document.getElementById('btnadd');
 utils.addEvent(btnadd,'click',addData);

 function addData(){
  $( "#dialog-form" ).dialog({
      width: 700,
      title:'添加酒水信息',
      modal:true,
      closeOnEscape:true,
      resizable:false,
      open:function(res){
          Initialization();
        },
      buttons:{
        "添加":function(){

          var submitform=document.getElementById('submitform'),
          data={};

          data.action="add";

          data.typeId=submitform.typeId.value;
          data.name=submitform.name.value;
          data.price=submitform.price.value;
          data.description=submitform.description.value;
          data.orderType=submitform.orderType.value;
          data.status=submitform.status.value;

          $.ajax({
              type:"post",
              url:"/configmgr/updateFood",
              data:data,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                  getDataList();
                  $("#dialog-form" ).dialog("close");
                }else{
                  alert(res.info);
                }
              }
          });

          
        },
        "取消":function(){
          $(this).dialog("close");
        }

      }
  });
 }

 function getDataList(index){
  var data={};
  data.page=index||0;
  data.numPerPage=pagesize;
  $.ajax({
    type:"post",
    url:"/configmgr/getFood",
    data:data,
    dataType:'json',   
    success:function(res){
      
      var data=res.data;
      var table=document.getElementById("dataList");
      var tbody= table.getElementsByTagName('tbody')[0];

      dataTable.clearData(tbody);
      
      for(var i=0;i<data.list.length;i++){
        addRow(tbody,'',data.list[i]);
      }
      
      if(!index||index==0){
        initPagination(data.count);
      }

    }
  });
}

function initPagination(page) {
  $("#pagination").pagination(page, {
      callback: pageselectCallback,
      items_per_page:pagesize, // 每页只显示一条记录
      num_display_entries : 3,
      num_edge_entries : 1, 
      prev_text : "上一页",  
      next_text : "下一页", 
  });
}

function pageselectCallback(page_index, jq){
    getDataList(page_index);
    return false;
}

function addRow(table,index,data){
  var row=table.insertRow();
  var cell=row.insertCell(0);
  var cell1=row.insertCell(1);
  var cell2=row.insertCell(2);
  var cell3=row.insertCell(3);
  var cell4=row.insertCell(4);
  var cell5=row.insertCell(5);
  var cell6=row.insertCell(6);
  var cell7=row.insertCell(7);
  var cell8=row.insertCell(8);

  
  cell.innerHTML=data.typeId;
  cell1.innerHTML=data.name;
  cell2.innerHTML=data.price;
  cell3.innerHTML=data.description;
  cell4.innerHTML=data.orderType;
  cell5.innerHTML=data.status;

  var deit=document.createElement("a"),
  del=document.createElement("a");
  
  deit.innerHTML='编辑';
  deit.className="table-edit";
  deit.data=data.id;
  utils.addEvent(deit,'click',dataEdit);

  del.innerHTML='删除';
  del.data=data.id; 
  utils.addEvent(del,'click',dataDelete); 

  cell6.appendChild(deit);
  cell6.appendChild(del);
    
  return row;
}


function dataEdit(){
  var btn=this;  
  var url="/configmgr/updateFood/"+btn.data;
  
   $( "#dialog-form" ).dialog({
        width: 700,
        title:'修改酒水信息',
        modal:true,
        closeOnEscape:true,
        resizable:false,
        open:function(){
          var data={};
          data.action='';

          Initialization();

          $.ajax({
            type:"post",
            data:data,
            url:url,
            dataType:'json',
            success:function(res){
              var data= res.data;

              var submitform=document.getElementById('submitform');
              submitform.typeId.value=data.typeId;
              submitform.name.value=data.name;
              submitform.price.value=data.price;
              submitform.description.value=data.description;
              submitform.orderType.value=data.orderType;
              submitform.status.value=data.status;
            }
          });
        },
        buttons:{
          "修改":function(){
            var submitform=document.getElementById('submitform'),
            data={};

            data.action="update";

            data.typeId=submitform.typeId.value;
            data.name=submitform.name.value;
            data.price=submitform.price.value;
            data.description=submitform.description.value;
            data.orderType=submitform.orderType.value;
            data.status=submitform.status.value;

            $.ajax({
              type:"post",
              data:data,
              url:url,
              dataType:'json',
              success:function(res){
                if(res.code==0){
                  getDataList();
                  $("#dialog-form" ).dialog("close");
                }else{
                  alert(res.info);
                }       
              }
            });
            
          },
          "取消":function(){
            $(this).dialog("close");
          }

        }
   });

  

}

function dataDelete(){
  if(confirm("确定要删除该数据")){
    var btn=this;  
    var url="/configmgr/updateFood/"+btn.data,
    data={};
    data.action="del";
    $.ajax({
      type:"post",
      data:data,
      url:url,
      dataType:'json',
      success:function(res){
        if(res.code==0){
          getDataList();
         //dataTable.delTableRow(btn);
        }else{
          alert(res.info);
        }       
      }
    });
  }
}

</script>
